﻿using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using TextAnalytics.Operations;
using TextAnalytics.Views;

namespace TextAnalytics
{
    public static class CustomCommands
    {
        public static NewProjectCommand NewProject = new NewProjectCommand();
        public static OpenProjectCommand OpenProject = new OpenProjectCommand();
        public static CloseProjectCommand CloseProject = new CloseProjectCommand();
        public static ExitCommand Exit = new ExitCommand();
    }

    public class ExitCommand : ICommand
    {
        public event EventHandler CanExecuteChanged = delegate { };

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            Application.Current.Shutdown();
        }
    }

    public class NewProjectCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged = (sender, args) => { };

        public void Execute(object parameter)
        {
            var fbd = new NewProjectDialog();
            var result = fbd.ShowDialog();
            if (result.HasValue && result.Value == true)
            {
                var projectModel = ProjectOperations.CreateNewProject(fbd.ViewModel.ProjectName, fbd.ViewModel.ProjectLocation);
                ProjectService.Default.OpenedProject = projectModel;
            }
        }
    }

    public class OpenProjectCommand : ICommand
    {
        public OpenProjectCommand()
        {
            ProjectService.Default.DidOpenProject += (sender, eventArgs) =>
            {
                CanExecuteChanged(this, EventArgs.Empty);
            };
            ProjectService.Default.DidCloseProject += (sender, eventArgs) =>
            {
                CanExecuteChanged(this, EventArgs.Empty);
            };
        }

        public bool CanExecute(object parameter)
        {
            return ProjectService.Default.HasOpenedProject == false;
        }

        public event EventHandler CanExecuteChanged = (sender, args) => { };

        public void Execute(object parameter)
        {
            var ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.Filter = "Text Analytics Project Files (*.taproj) | *.taproj";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var result = ofd.ShowDialog();
            if (result.HasValue && result.Value)
            {
                var projectModel = ProjectOperations.OpenExistingProject(ofd.FileName);
                ProjectService.Default.OpenedProject = projectModel;
            }
        }
    }

    public class CloseProjectCommand : ICommand
    {
        public CloseProjectCommand()
        {
            ProjectService.Default.DidOpenProject += (sender, eventArgs) =>
            {
                CanExecuteChanged(this, EventArgs.Empty);
            };
            ProjectService.Default.DidCloseProject += (sender, eventArgs) =>
            {
                CanExecuteChanged(this, EventArgs.Empty);
            };
        }

        public bool CanExecute(object parameter)
        {
            return ProjectService.Default.HasOpenedProject;
        }

        public event EventHandler CanExecuteChanged = delegate { };

        public void Execute(object parameter)
        {
            var projectService = ProjectService.Default;
            projectService.CloseOpenedProject();
        }
    }
}
